package com.peace.hospital.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.peace.hospital.entity.common.Department;

import com.peace.hospital.entity.common.Staffinfo;
import com.peace.hospital.entity.system.Position;
import com.peace.hospital.mapper.StaffinfoMapper;
import com.peace.hospital.service.IStaffinfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * <p>
 * 医院员工信息表 服务实现类
 * </p>
 *
 * @author xie
 * @since 2019-11-12
 */
@Service
public class StaffinfoServiceImpl extends ServiceImpl<StaffinfoMapper, Staffinfo> implements IStaffinfoService {
    @Autowired
    private StaffinfoMapper staffinfoMapper;

    //查询用户信息
    @Override
    public List<Staffinfo> SelectStaffinfo() {
        List<Staffinfo> staffinfoList = staffinfoMapper.SelcctStaffinfoMessage();
//         List<Staffinfo> list =null;
//        for (Staffinfo staffinfo : staffinfoList) {
//            String stanstate = staffinfo.getStanstate();
//
//            if (stanstate.equals("在职")) {
//               list.add(staffinfo);
//               return list;
//            }
//        }
        return staffinfoList;
    }

    //条件查询用户信息
    @Override
    public List<Staffinfo> SelectStaffinfoBystajobnumberOrStaname(Staffinfo staffinfo) {
        List<Staffinfo> list= staffinfoMapper.SelectLike(staffinfo);
        if(null!=list){
            return  list;
        }
        return null;
    }


    //添加加员工
    @Override
    public boolean addStaffinfo(Staffinfo staffinfo, Position position, Department department) {

        int i=  staffinfoMapper.SelectStaid();
        staffinfo.setStajobnumber(i+1);

        //查询用户是否存在
        Staffinfo staffinfos = staffinfoMapper.slectByStatel(staffinfo.getStatel());


        if (null==staffinfos) {
            //根据用户传入的科室类型查询科室ID
            Department depids = staffinfoMapper.SelcctDepid(department.getDeptype());

            int dep = depids.getDepid();
            //将科室ID set入用户类
            staffinfo.setDepid(dep);


            //向用户表添加一个用户
            staffinfoMapper.InsertStaffinfo(staffinfo);
            Staffinfo staff = staffinfoMapper.slectByStatel(staffinfo.getStatel());
            //获取这个用户ID
            int stids = staff.getStaid();
            //根据用户ID和职位ID修改中间表对应关系p
            List<Integer> list = position.getPostitleList();
            int sa = 0;
            for (Integer integer : list) {
                System.out.println(integer+"*****"+stids);
                sa = staffinfoMapper.InsertMassge(integer, stids);
            }
            //修改中间表关系

            if (sa > 0) {
                return true;
            }
            return false;
        }
        return false;
    }


    //修改用户信息
    @Transactional
    @Override
    public boolean UpdateStaffinfo(Staffinfo staffinfo, Position position, Department department) {
//        System.out.println(staffinfo+"****************");
//        System.out.println(position);
//        System.out.println(department);

        //根据用户传入的科室类型查询科室ID
        Department depid = staffinfoMapper.SelcctDepid(department.getDeptype());
        int dep = depid.getDepid();
        //将科室ID摄入用户类
         staffinfo.setDepid(dep);
        //修改用户表
        int staf = staffinfoMapper.UpdateStaffinfo(staffinfo);
        //获取用户ID
        int stid = staffinfo.getStaid();
        //根据用户ID和职位ID修改中间表对应关系p
        staffinfoMapper.deletestaffinfoandposition(staffinfo.getDepid());
        List<Integer> list = position.getPostitleList();
        int sa = 0;
        for (Integer integer : list) {
            sa = staffinfoMapper.InsertMassge(integer, stid);
        }
        //修改中间表关系

        if (staf > 0 || sa > 0) {
            return true;
        }
        return false;
    }


    //删除员工
    @Override
    public boolean DeleteStaffinfo(Integer staid) {
        int i = staffinfoMapper.UpdateStanstate(staid);
        if (i > 0) {
            return true;
        }
        return false;
    }
}
